package com.xxx.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxx.common.Result;
import com.xxx.entity.MPage;
import com.xxx.entity.Survey;
import com.xxx.entity.query.SurveyQuery;
import com.xxx.service.ISurveyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xxx
 * @since 2024-11-24
 */
@RestController
@RequestMapping("/survey")
@Tag(name = "市场调研管理")
public class SurveyController {

    @Autowired
    private ISurveyService surveyService;

    @PostMapping("/add")
    @Operation(summary = "添加调研")
    public Result add(@RequestBody Survey survey){
        Boolean flag = surveyService.save(survey);
        return flag ? Result.success("添加调研成功") : Result.error("添加调研失败");
    }

    @GetMapping("/delete/{surveyId}")
    @Operation(summary = "删除调研")
    public Result delete(@PathVariable Integer surveyId){
        Boolean flag = surveyService.removeById(surveyId);
        return flag ? Result.success("删除调研成功") : Result.error("不存在该调研Id");
    }

    @PutMapping("/update")
    @Operation(summary = "修改调研")
    public Result update(@RequestBody Survey survey){
        Boolean flag = surveyService.updateById(survey);
        return flag ? Result.success("修改调研成功") : Result.error("修改调研失败");
    }

    @GetMapping("/selectByPage/{current}/{size}")
    @Operation(summary = "分页查询调研")
    public Result selectByPage(@PathVariable Integer current, @PathVariable Integer size){
//        IPage<Survey> page = surveyService.page(new Page<>(current,size));
        MPage page = surveyService.selectByPage(current, size);
        return Result.success(page);

    }

    @PostMapping("/selectByCondition")
    @Operation(summary = "根据用户Id、调研Id、用户名、用户手机号条件查询调研")
    public Result selectByCondition(@RequestBody SurveyQuery surveyQuery){
//        LambdaQueryWrapper<Survey> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(surveyQuery.getUserId()!=null,Survey::getUserId,surveyQuery.getUserId())
//                .eq(surveyQuery.getSurveyId()!=null,Survey::getSurveyId,surveyQuery.getSurveyId());
//        IPage<Survey> page = surveyService.page(new Page<>(surveyQuery.getCurrent(), surveyQuery.getSize()),
//                lqw);
        MPage page = surveyService.selectByCondition(surveyQuery);
        return Result.success(page);
    }


    @GetMapping("/incomeAnalysis")
    @Operation(summary = "收入分析接口")
    public Result incomeAnalysis(){
        Map<String, Object> map = surveyService.incomeAnalysis();

        return Result.success(map);
    }
}

